<template>
  <div class="app-container">
    <ai_nabar style="margin: 10px 0 10px 0px;" index="3"></ai_nabar>
    <!-- 这是最顶部 -->
    <div class="top" style="border-bottom: 1px solid rgb(223, 231, 245); margin-bottom: 20px; height: auto">
      <h3 style="font-weight: bold;">模型选择</h3>
      <p style="font-size: 12px; color: rgb(202, 203, 204);">这些是您首选LLM聊天和嵌入提供商的凭据和设置，请确保这些内容都是正确的，否则LLM将无法正常工作</p>
    </div>
    <div class="one">
      <h2>LLM供应</h2>
      <el-form  class="frominfo" ref="queryForm" size="small" :inline="true" v-show="showSearch"
        label-width="68px">
        <el-form-item prop="wxLabel">
          <el-input v-model="llmName" @keyup.enter.native="handleQuery" placeholder="请输入LLM供应" />
        </el-form-item>
      </el-form>
    </div>
    <div class="two">
      <h2>Ollama基本URL</h2>
      <el-form class="frominfo" ref="queryForm" size="small" :inline="true" v-show="showSearch"
        label-width="68px">
        <el-form-item prop="wxLabel">
          <el-input v-model="llmUrl" @keyup.enter.native="handleQuery" placeholder="请输入Url" />
        </el-form-item>
      </el-form>
    </div>
    <div class="three">
      <h2>聊天模式选择</h2>
      <el-form  class="frominfo" ref="queryForm" size="small" :inline="true" v-show="showSearch"
        label-width="68px">
        <el-form-item prop="wxLabel">
          <el-input v-model="chatType" @keyup.enter.native="handleQuery" placeholder="请输入聊天模式" />
        </el-form-item>
      </el-form>
    </div>
    <div class="four">
      <h2>令牌上下文窗口</h2>
      <el-form class="frominfo" ref="queryForm" size="small" :inline="true" v-show="showSearch"
        label-width="68px">
        <el-form-item prop="wxLabel">
          <el-input v-model="llmsize" @keyup.enter.native="handleQuery" placeholder="请设置窗口大小" />
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>

import request from '@/utils/request'
import ai_nabar from '../components/ai_nabar.vue'

export default {
  components: {
    ai_nabar
  },
  dicts: ['sys_friend_proport'],
  name: "WcModelSetup",
  data() {
    return {
      llmName: "",  // 用于存储llmName
      llmUrl: "",  // 用于存储llmUrl
      updateBy: "",  // 用于存储updateBy
      width: "",  // 用于存储width
      height: "",  // 用于存储height
      llmsize:"",
      chatType:"",
      id: '',

      orderNum: ["文本1", "文本2", "文本3"],
      selectedIndex: 0,// 初始选中的索引

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模型设置表格数据
      wcModelSetupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wcId: null,
        llmName: null,
        llmUrl: null,
        chatType: null,
        height: null,
        width: null,
      },
    };
  },
  created() {
    // this.getList();
    this.id = this.$store.state.user.wxid;
    this.getFriendDetails(this.id);
  },

  methods: {
    formatValue(value) {
      // 确保值在数组范围内
      if (value >= 0 && value < this.orderNum.length) {
        return this.orderNum[value];
      }
      return value;
    },
    parseValue(value) {
      // 将文本内容转换回数字索引
      const index = this.orderNum.indexOf(value);
      // 如果找到了对应的文本，返回索引，否则返回原值
      return index !== -1 ? index : value;
    },
    // 模型设置-通过wx_id查询
    getFriendDetails(id) {

      getFriendDetails(id).then(res => {
        if (res) {
          //console.log(res.data, 'sssssssssss');
          this.llmName = res.data.llmName;
          this.llmUrl = res.data.llmUrl;
          this.updateBy = res.data.updateBy;
          this.width = res.data.width;
          this.height = res.data.height;
          this.llmsize=res.data.width+'x'+res.data.height
        }else {
          //console.log('失败了');
        }
      });
    },
    /** 查询模型设置列表 */
    getList() {
      this.loading = true;
      listWcModelSetup(this.queryParams).then(res => {
        if (res['success']) {
          const dt = res['data'];
          this.total = dt['total'];
          this.wcModelSetupList = dt['records'];
        }
        this.loading = false;
      });
      setTimeout(()=>{
        this.loading = false;
      },2000)
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        wcId: null,
        llmName: null,
        llmUrl: null,
        chatType: null,
        height: null,
        width: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模型设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWcModelSetup(id).then(response => {
        console.log(response, '看这里');
        // this.form = response.data;
        // this.open = true;
        // this.title = "修改模型设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWcModelSetup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWcModelSetup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除模型设置编号为"' + ids + '"的数据项？').then(function () {
        return delWcModelSetup(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wxassistant/wcmodel_setup/export', {
        ...this.queryParams
      }, `wcModelSetup_${new Date().getTime()}.xlsx`)
    }
  },
  watch: {
    selectedIndex(newValue) {
      // 确保selectedIndex的值在orderNum数组的有效范围内
      if (newValue < 0 || newValue >= this.orderNum.length) {
        this.selectedIndex = 0; // 设置为默认值
      }
    }
  }

};

/** js请求 调用 */
// 模型设置-通过wx_id查询
export function getFriendDetails(id) {
  return request({
    url: '/wxassistant/wcmodel_setup/queryWxId/' + id,
    method: 'get'
  })
}
// 查询模型设置列表
export function listWcModelSetup(query) {
  return request({
    url: '/wxassistant/wcmodel_setup/page',
    method: 'get',
    params: query
  })
}

// 查询模型设置详细
export function getWcModelSetup(id) {
  return request({
    url: '/wxassistant/wcmodel_setup/queryById/' + id,
    method: 'get'
  })
}

// 新增模型设置
export function addWcModelSetup(data) {
  return request({
    url: '/wxassistant/wcmodel_setup/add',
    method: 'post',
    data: data
  })
}

// 修改模型设置
export function updateWcModelSetup(data) {
  return request({
    url: '/wxassistant/wcmodel_setup/edit',
    method: 'put',
    data: data
  })
}

// 删除模型设置
export function delWcModelSetup(id) {
  return request({
    url: '/wxassistant/wcmodel_setup/delete/' + id,
    method: 'delete'
  })
}

</script>
